package cn.zb.zbrhback.org.service.impl;

import cn.zb.zbrhback.basic.service.impl.BaseServiceImpl;
import cn.zb.zbrhback.org.domain.PageQuery;
import cn.zb.zbrhback.basic.util.PageResult;
import cn.zb.zbrhback.org.domain.Department;
import cn.zb.zbrhback.org.mapper.DepartmentMapper;
import cn.zb.zbrhback.org.service.IDepartmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Resource
    private DepartmentMapper departmentMapper;
    @Transactional
    @Override
    public void add(Department department) {
        department.setCreateTime(new Date());

        departmentMapper.insert(department);
        System.out.println("插入成功"+department);
        if(department.getParent().getId() != null){
            Department parent = departmentMapper.loadById(department.getParent().getId());
            System.out.println("父部门"+parent);
            String path = parent.getPath() + "/" + department.getId();
            department.setPath(path);
        }else {
            String path = "/" + department.getId();
            department.setPath(path);
        }
        departmentMapper.update(department);
    }

    @Transactional
    @Override
    public void update(Department department) {
        department.setUpdateTime(new Date());
        if(department.getParent().getId() != null){
            Department parent = departmentMapper.loadById(department.getParent().getId());
            String path = parent.getPath() + "/" +department.getId();
            department.setPath(path);
        }

        departmentMapper.update(department);

    }
}
